package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.CollectionAccountPageQuery;
import com.eastfair.pay.dto.CollectionAccountSaveDTO;
import com.eastfair.pay.dto.CollectionAccountUpdateDTO;
import com.eastfair.pay.entity.CollectionAccount;
import com.eastfair.pay.service.CollectionAccountService;
import com.eastfair.pay.vo.CollectionAccountVO;
import com.eastfair.projectcore.enumeration.MenuAffiliationEnum;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 收款账户
 * </p>
 *
 * @author clm
 * @date 2023-02-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/collectionAccount")
@Api(value = "CollectionAccount", tags = "收款账户")
@PreAuth(replace = "pay:collectionAccount:")
public class CollectionAccountController extends SuperController<CollectionAccountService, Long, CollectionAccount, CollectionAccountPageQuery, CollectionAccountSaveDTO, CollectionAccountUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Value("${pay.payee.venue-manage}")
    private String venueManagePayee;

    @Value("${pay.payee.venue-booking}")
    private String venueBookingPayee;

    @Override
    public void handlerResult(IPage<CollectionAccount> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<CollectionAccount> collectionAccountList = list.stream().map((map) -> {
            CollectionAccount collectionAccount = CollectionAccount.builder().build();
            //TODO 请在这里完成转换
            return collectionAccount;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(collectionAccountList));
    }

    @Override
    public R<CollectionAccount> save(CollectionAccountSaveDTO collectionAccountSaveDTO) {
        return R.success(baseService.saveCollectionAccount(collectionAccountSaveDTO));
    }

    /**
     * @Author fwl
     * @Description //通过系统标识获取收款账户
     * @Date 15:58 2023/3/24
     * @Param [systemScene]
     **/
    @GetMapping("/getCollectionAccountBySystemScene")
    R<String> getCollectionAccountBySystemScene(String systemScene) {
        return R.success(baseService.getCollectionAccountBySystemScene(systemScene));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CollectionAccountPageQuery> params) {
        CollectionAccountPageQuery pageQuery = params.getModel();

    }

    /**
     * @return com.eastfair.core.base.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page < com.eastfair.pay.vo.CollectionAccountVO>>
     * @Author clm
     * @Description //分页查询收款账户
     * @Date 10:15 2023/2/22
     * @Param [params]
     **/
    @PostMapping("/queryList")
    public R<Page<CollectionAccountVO>> queryList(@RequestBody PageParams<CollectionAccountPageQuery> params) {

        try {
            return R.success(baseService.queryList(params));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 查询配置的收款方
     *
     * @param systemCode the system code
     * @return the r
     */
    @GetMapping("/query/config/payee")
    public R<String> queryConfigPayee(@RequestParam("systemCode") String systemCode) {
        log.info("queryConfigPayee - 查询配置的收款方, systemCode={}", systemCode);
        if (MenuAffiliationEnum.P_COMPOSITE_VENUE.getCode().equals(systemCode)) {
            return R.successDef(venueManagePayee);
        }
        if (MenuAffiliationEnum.P_VENUE.getCode().equals(systemCode)) {
            return R.successDef(venueBookingPayee);
        }
        return R.successDef();
    }

    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //逻辑删除
     * @Date 11:06 2023/2/22
     * @Param [collectionAccountDTO]
     **/
    @PostMapping("/delete")
    public R delete(@RequestBody CollectionAccount collectionAccount) {
        try {
            baseService.delete(collectionAccount);
            return R.success(Boolean.TRUE);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 获取收款账户集合
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<CollectionAccountVO>> list() {
        try {
            List<CollectionAccount> list = baseService.lambdaQuery().eq(CollectionAccount::getIsDeleted, BusinessConstant.DELETE_NO).eq(CollectionAccount::getIsEnabled, BusinessConstant.ENABLE_YES).list();
            List<CollectionAccountVO> collect = list.stream().map(l -> {
                CollectionAccountVO vo = new CollectionAccountVO();
                BeanUtils.copyProperties(l, vo);
                return vo;
            }).collect(Collectors.toList());
            return R.success(collect);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }
}
